package fr.cephb.dbsnptools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import fr.cephb.json.JSONBuilder;
import fr.cephb.json.JSONable;

public class SnpRecord
	implements Serializable,JSONable,Comparable<SnpRecord>
	{
	private static final long serialVersionUID = 1L;
	private int rsId=-1;
	private String seq5=null;
	private String seq3=null;
	private String observed=null;
	private Set<String> handles=new HashSet<String>();
	private List<MapLoc> locations=new ArrayList<MapLoc>();
	
	public static class FxnSet
		implements JSONable
		{
		public String fxnClass=null;;
		public String geneId=null;;
		public String mrnaAcc=null;;
		public String mrnaVer=null;;
		public String symbol=null;
		public String aaPosition=null;
		public String allele=null;
		public String protAcc=null;
		public String protVer=null;
		public String residue=null;
		public String readingFrame=null;
		
		@Override
		public String toJSON()
			{
			return new JSONBuilder().
				put("fxnClass", fxnClass).
				put("geneId", geneId).
				put("mrnaAcc", mrnaAcc).
				put("mrnaVer", mrnaVer).
				put("symbol", symbol).
				put("aaPosition", aaPosition).
				put("allele", allele).
				put("protAcc", protAcc).
				put("protVer", protVer).
				put("residue", residue).
				put("readingFrame", readingFrame).
				
				
				getString();
			}
		@Override
		public String toString() {
			return toJSON();
			}
		}
	
	public static class MapLoc
		{
		public List<FxnSet> fxn= new ArrayList<FxnSet>();
		public String groupLabel;
		public String genomeBuild=null;
		public String chromosome=null;
		public String orient=null;
		public Integer physMapInt=null;
		public Integer leftContigNeighborPos=null;
		public Integer rightContigNeighborPos=null;
		}
	

	
	public void setRsId(int rsId)
		{
		if(rsId<=0) throw new IllegalArgumentException("rs"+rsId);
		this.rsId = rsId;
		}
	public int getRsId()
		{
		return rsId;
		}
	
	public String getSeq5()
		{
		return seq5;
		}
	
	public String getSeq5(int length)
		{
		length=Math.min(seq5.length(), length);
		return seq5.substring(seq5.length()-length,seq5.length());
		}
	
	public void setSeq5(String seq5)
		{
		this.seq5 = seq5;
		}
	
	public void setSeq3(String seq3)
		{
		this.seq3 = seq3;
		}
	public String getSeq3()
		{
		return seq3;
		}
	
	public String getSeq3(int length)
		{
		length=Math.min(seq3.length(), length);
		return seq3.substring(0,length);
		}
	public String getObserved()
		{
		return observed;
		}
	
	public void setObserved(String observed)
		{
		this.observed = observed;
		}
	
	public Set<String> getHandles()
		{
		return handles;
		}
	
	public List<MapLoc> getLocations()
		{
		return locations;
		}
	
	@Override
	public String toJSON()
		{
		return new JSONBuilder().
			put("rs", getRsId()).
			put("seq5", getSeq5()).
			put("obs", getObserved()).
			put("seq3", getSeq3()).
			put("handles", getHandles()).
			getString();
		}
	
	@Override
	public int compareTo(SnpRecord o)
		{
		return getRsId()-o.getRsId();
		}
	
	@Override
	public String toString()
		{
		return toJSON();
		}
	}
